package config

import (
	"errors"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

type Rpc struct {
	TracingEngineAddr string `yaml:"tracing_engine_addr"` // ip/domain
	Id                string `yaml:"id"`                  // id/name of analyzer
	AuthKey           string `yaml:"auth_key"`            // auth key needed to access tracing engine
	ListenAddr        string `yaml:"listen_addr"`         // e.g. localhost:50051
	SelfKey           string `yaml:"self_key"`            // auth key for honeyport & tracker to access analyzer
}

type MachineLearning struct {
	ServerAddr string `yaml:"server_addr"`
}

type Config struct {
	RpcConf             Rpc             `yaml:"rpc"`
	MachineLearningConf MachineLearning `yaml:"ml"`
}

func (c *Config) Parse(path string) (*Config, error) {
	configFile, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	// check file existence
	if _, existErr := os.Stat(path); existErr != nil && os.IsNotExist(existErr) {
		return nil, errors.New("config file does not exist at " + path)
	}
	// parse file
	err = yaml.Unmarshal(configFile, c)
	if err != nil {
		return nil, errors.New("config file cannot be parsed, check your syntax ")
	}
	return c, err
}

func (c *Config) Validate() error {
	return nil
}
